/**
 * 无参Get请求
 * @param {String} url API地址
 * @param {Function} funSuccess 成功回调
 * @param {Function} funComplete 请求结束回调
 */
function get(url,funSuccess,funComplete){
	uni.showLoading({
		title:"加载中......",
		mask:false
	});
	uni.request({
		url:url,
		method:"GET",
		sslVerify:false,
		header:{
			"content-type":"application/json;charset=UTF-8"
		},
		success: res => {
			funSuccess(res.data)
		},
		fail: err => {
			console.log(err);
		},
		complete: () => {
			uni.hideLoading();
			funComplete()
		}
	});
}
/**
 * get请求，需要用户传递header
 * @param {String} url API地址
 * @param {JSON} header 请求头，包含参数，无参就用get方法
 * @param {Function} funSuccess
 * @param {Object} funComplete 请求结束的回调，如无需处理则传一个空方法就好 
 */
function getWithHeader(url,header,funSuccess,funComplete){
	uni.showLoading({
		title:"加载中......",
		mask:false
	});
	uni.request({
		url,
		header,
		method:"GET",
		sslVerify:false,
		success: res => {
			funSuccess(res.data)
		},
		fail: err => {
			// funfail(err)
			console.log(err);
			
			},
		complete: () => {
			uni.hideLoading();
			funComplete()
		}
	})
}

/***
 *
 * @param url
 * @param data
 * @param funSuccess
 * @param funComplete
 */
function postParam(url,data,funSuccess,funComplete){
	uni.showLoading({
		title:'提交数据',
		mask:false
	});
	uni.request({
		url:url,
		header:{
			"Accept":"application/json;q=0.9,*/*;q=0.1",
			// "content-type":"application/json;charset=UTF-8"  别写成这个
			"Content-Type":"application/x-www-form-urlencoded"
		},
		method:"POST",
		sslVerify:false,
		data:data,
		success: res => funSuccess(res.data),
		fail: err => {
			console.log(err);
		},
		complete: () => {
			uni.hideLoading();
			funComplete()
		}
	})
}
function post(url,data,funSuccess,funComplete){
	uni.showLoading({
		title:'提交数据',
		mask:false
	});
	uni.request({
		url:url,
		method:"POST",
		data:data,
		success: res => funSuccess(res.data),
		fail: err => {
			console.log(err);
		},
		complete: () => {
			uni.hideLoading();
			funComplete()
		}
	})
}

/**
 *
 * @param {String} url API地址
 * @param  data
 * @param {Function}
 * @param funComplete
 */
function del(url,data,funSuccess,funComplete){
	uni.request({
		url:url,
		header:{
			"Accept":"application/json;q=0.9,*/*;q=0.1",
			// "content-type":"application/json;charset=UTF-8"  别写成这个
			"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"
		},
		method:"DELETE",
		sslVerify:false,
		data:data,
		success: res => funSuccess(res.data),
		fail: err => {
			console.log(err);
		},
		complete: () => {
			funComplete()
		}
	})
}

/**上传文件
 * @param {String} url 服务器地址
 * @param {String} param 表单提交的name属性
 * @param {String} filePath 要上传的本地文件路径
 * @param {JSON} data 其他同时提交的表单参数
 * @param {Function} funSuccess 成功的回调
 * @param {Function} funComplete 结束的回调
 */
function postFile(url, param, filePath, data, funSuccess, funComplete) {
	uni.showLoading({
		title: '上传头像',
		mask: false
	});
	uni.uploadFile({
		url: url,
		sslVerify: false,
		filePath: filePath,
		name: param,
		formDate: data,
		success: res => funSuccess(res),
		fail: err => {
			console.log(err);
		},
		complete:()=> {
			uni.hideLoading();
			funComplete()
		}
	});
}
function requestAsync(url, data={}, method='GET'){
	return new Promise((resolve, rejects) => {
		uni.request({
			url: url,
			method: method,
			data: data,
			success: res => {
				resolve(res)
			},
			fail: err => {
				rejects(err)
			},
			complete: () => {}
		});
	})
}

// 导出
module.exports={
	get,
	getWithHeader,
	postParam,
	post,
	del,
	postFile,
	requestAsync
}